package view;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

/**
 * This Class is a registration window.
 *
 * @author Dmitry
 */
public class UserRegisterGUI extends AbstractGUI {
	
	/** The lbl first name. */
	private JLabel lblFirstName = null;
	
	/** The lbl last name. */
	private JLabel lblLastName = null;
	
	/** The lbl password. */
	private JLabel lblPassword = null;
	
	/** The lbl email. */
	private JLabel lblEmail = null;
	
	/** The lbl phone. */
	private JLabel lblPhone = null;
	
	/** The lbl address. */
	private JLabel lblAddress = null;
	
	/** The txt first name. */
	private JTextField txtFirstName = null;
	
	/** The txt last name. */
	private JTextField txtLastName = null;
	
	/** The txt password. */
	private JTextField txtPassword = null;
	
	/** The txt address. */
	private JTextField txtAddress = null;
	
	/** The formatted txt phone. */
	private JFormattedTextField formattedTxtPhone = null;
	
	/** The txt email. */
	private JTextField txtEmail = null;
	
	/** The combo box email. */
	private JComboBox comboBoxEmail = null;
	
	/** The btn register. */
	private JButton btnRegister = null;
	
	/** The lbl wrn message. */
	private JLabel lblWrnMessage = null;
	
	/** The panel. */
	private JPanel panel;

	/**
	 * This is the constructor.
	 */
	public UserRegisterGUI() {
		getContentPane().setLayout(null);
		getContentPane().add(getLblFirstName());
		getContentPane().add(getLblLastName());
		getContentPane().add(getLblPassword());
		getContentPane().add(getLblEmail());
		getContentPane().add(getLblPhone());
		getContentPane().add(getLblAddress());
		getContentPane().add(getTxtFirstName());
		getContentPane().add(getTxtLastName());
		getContentPane().add(getTxtPassword());
		getContentPane().add(getFormattedTxtPhone());
		getContentPane().add(getTxtAddress());
		getContentPane().add(getTxtEmail());
		getContentPane().add(getComboBoxEmail());
		getContentPane().add(getBtnRegister());
		getContentPane().add(getBtnCancel());
		getContentPane().add(getLblWrnMessage());
		getContentPane().add(getPanel());
		
		this.setTitle("Registration");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setBounds(500, 200, 335, 400);
		this.setVisible(true);
		this.setResizable(false);
	}

	/**
	 * This method both creates and returns a "Register" button.
	 * @return instance of Register button.
	 */
	private JButton getBtnRegister() {
		if(btnRegister == null){
			btnRegister = new JButton("Register");
			btnRegister.setBounds(70, 338, 89, 23);
		}
		return btnRegister;
	}
	
	/**
	 * This method both creates and returns a combo box instance. 
	 * @return a ComboBox instance.
	 */
	private JComboBox getComboBoxEmail() {
		if(comboBoxEmail == null){
			comboBoxEmail = new JComboBox();
			comboBoxEmail.setBounds(218, 83, 88, 20);
			comboBoxEmail.addItem("@gmail.com");
			comboBoxEmail.addItem("@walla.com");
			comboBoxEmail.addItem("@outlook.com");
		}
		return comboBoxEmail;
	}

	/**
	 * This method both creates and returns an Address text field instance.
	 * @return a JTextField instance.
	 */
	private JTextField getTxtAddress() {
		if(txtAddress == null){
			txtAddress = new JTextField();
			txtAddress.setBounds(87, 133, 219, 20);
			txtAddress.setColumns(10);
			}
		return txtAddress;
	}

	/**
	 * This method both creates and returns a Phone formatted text field instance.
	 * @return JFormatted text field.
	 */
	private JFormattedTextField getFormattedTxtPhone() {
		if(formattedTxtPhone == null){
			formattedTxtPhone = new JFormattedTextField(createFormatter("###-#######"));
			formattedTxtPhone.setBounds(87, 108, 219, 20);
		}
		return formattedTxtPhone;
	}

	/**
	 * This method both creates and returns a Last Name label instance.
	 * @return JLabel instance.
	 */
	private JLabel getLblLastName() {
		if (lblLastName == null) {
			lblLastName = new JLabel("Last Name");
			lblLastName.setBounds(10, 36, 67, 14);
		}
		return lblLastName;
	}

	/**
	 * This method both creates and returns a First Name label instance.
	 * @return JLabel instance.
	 */
	private JLabel getLblFirstName() {
		if (lblFirstName == null) {
			lblFirstName = new JLabel("First Name");
			lblFirstName.setBounds(10, 11, 67, 14);
		}
		return lblFirstName;
	}

	/**
	 * This method both creates and returns a Password label instance.
	 * @return JLabel instance.
	 */
	private JLabel getLblPassword() {
		if (lblPassword == null) {
			lblPassword = new JLabel("Password");
			lblPassword.setBounds(10, 61, 67, 14);
		}
		return lblPassword;
	}

	/**
	 * This method both creates and returns an Email label instance.
	 * @return JLabel instnace.
	 */
	private JLabel getLblEmail() {
		if (lblEmail == null) {
			lblEmail = new JLabel("EMail");
			lblEmail.setBounds(10, 86, 67, 14);
		}
		return lblEmail;
	}

	/**
	 * This method both creates and returns a Phone label instance.
	 * @return JLabel instance.
	 */
	private JLabel getLblPhone() {
		if (lblPhone == null) {
			lblPhone = new JLabel("Phone");
			lblPhone.setBounds(10, 111, 67, 14);
		}
		return lblPhone;
	}

	/**
	 * This method both creates and returns Address label instance.
	 * @return JLabel instance.
	 */
	private JLabel getLblAddress() {
		if (lblAddress == null) {
			lblAddress = new JLabel("Address");
			lblAddress.setBounds(10, 136, 67, 14);
		}
		return lblAddress;
	}

	/**
	 * This method both creates and returns First Name text field instance.
	 * @return JTextField instance.
	 */
	private JTextField getTxtFirstName() {
		if (txtFirstName == null) {
			txtFirstName = new JTextField();
			txtFirstName.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					char c = e.getKeyChar();
					if((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_SPACE)))
						e.consume();
				}
			});
			
			txtFirstName.setBounds(87, 8, 219, 20);
			txtFirstName.setColumns(10);
		}
		return txtFirstName;
	}

	/**
	 * This method both creates and returns Last Name text field instance.
	 * @return JTextField instance.
	 */
	private JTextField getTxtLastName() {
		if (txtLastName == null) {
			txtLastName = new JTextField();
			txtLastName.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					char c = e.getKeyChar();
					if((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_SPACE)))
						e.consume();
				}
			});
			txtLastName.setBounds(87, 33, 219, 20);
			txtLastName.setColumns(10);
		}
		return txtLastName;
	}

	/**
	 * This method both creates and returns Password text field instance.
	 * @return JTextField instance.
	 */
	private JTextField getTxtPassword() {
		if (txtPassword == null) {
			txtPassword = new JTextField();
			txtPassword.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					char c = e.getKeyChar();
					if(c == KeyEvent.VK_SPACE){
						getTxtPassword().setToolTipText("You can't use space in password");
						e.consume();
					}
				}
			});
			txtPassword.setBounds(87, 58, 219, 20);
			txtPassword.setColumns(10);
		}
		return txtPassword;
	}

	/**
	 * This method both creates and returns Email text fields instance.
	 * @return JTextField instance.
	 */
	private JTextField getTxtEmail() {
		if (txtEmail == null) {
			txtEmail = new JTextField();
			txtEmail.setBounds(87, 83, 121, 20);
			txtEmail.setColumns(10);
		}
		return txtEmail;
	}
	
	/**
	 * This method both creates and returns Cancel button instance.
	 * @return JButton instance.
	 */
	private JButton getBtnCancel() {
		if (backButton == null) {
			backButton = new JButton("Cancel");
			backButton.setBounds(169, 338, 89, 23);
		}
		return backButton;
	}
	
	/**
	 * This method both creates and returns warning message label instance.
	 * @return JLabel instance.
	 */
	private JLabel getLblWrnMessage() {
		if (lblWrnMessage == null) {
			lblWrnMessage = new JLabel("You MUST fill all required fields!");
			lblWrnMessage.setForeground(Color.RED);
			lblWrnMessage.setBounds(10, 186, 296, 20);
			lblWrnMessage.setVisible(false);
		}
		return lblWrnMessage;
	}
	
	/**
	 * This method both creates and returns window's panel instance.
	 * @return JPanel instance.
	 */
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Required Fields", TitledBorder.CENTER, TitledBorder.BOTTOM, null, Color.BLUE));
			panel.setBounds(0, 0, 329, 175);
		}
		return panel;
	}
	
	/**
	 * Gets the first name.
	 *
	 * @return the first name
	 */
	public String getFirstName(){
		return getTxtFirstName().getText();
		}
	
	/**
	 * Gets the last name.
	 *
	 * @return the last name
	 */
	public String getLastName(){
		return getTxtLastName().getText();
	}
	
	/**
	 * Gets the password.
	 *
	 * @return the password
	 */
	public String getPassword(){
		return getTxtPassword().getText();
	}
	
	/**
	 * Gets the email.
	 *
	 * @return the email
	 */
	public String getEmail(){
		return getTxtEmail().getText() + getComboBoxEmail().getSelectedItem().toString();
	}
	
	/**
	 * Gets the phone.
	 *
	 * @return the phone
	 */
	public String getPhone(){
		return getFormattedTxtPhone().getText();
	}
	
	/**
	 * Gets the address.
	 *
	 * @return the address
	 */
	public String getAddress(){
		return getTxtAddress().getText();
	}
	
	/**
	 * Display warning message.
	 *
	 * @param msg the message which will be displayed.
	 */
	public void displayWrnMessage(String msg){
		getLblWrnMessage().setText(msg);
		getLblWrnMessage().setVisible(true);
	}
	
	/**
	 * Undisplay warning message - make the warning message disappear.
	 */
	public void undisplayWrnMessage(){
		getLblWrnMessage().setVisible(false);
	}
	
	/**
	 * Clear fields.
	 */
	public void clearFields(){
		getTxtFirstName().setText("");
		getTxtLastName().setText("");
		getTxtPassword().setText("");
		getTxtEmail().setText("");
		getFormattedTxtPhone().setText("");
		getTxtAddress().setText("");
		
	}
	
	/**
	 * Adds the register action listener.
	 *
	 * @param listener the listener
	 */
	public void addRegisterActionListener(ActionListener listener){
		getBtnRegister().addActionListener(listener);
	}
	
	/**
	 * Adds the cancel action listener.
	 *
	 * @param listener the listener
	 */
	public void addCancelActionListener(ActionListener listener){
		getBtnCancel().addActionListener(listener);
	}
	
	/**
	 * Creates the formatter.
	 *
	 * @param s formatted in a string type
	 * @return the mask formatter
	 */
	private MaskFormatter createFormatter(String s) {
		MaskFormatter formatter = null;
		try {
			formatter = new MaskFormatter(s);
		} catch (java.text.ParseException exc) {
			System.err.println("formatter is bad: " + exc.getMessage());
			System.exit(-1);
		}
		return formatter;
	}
	
	/**
	 * This method creates an artificial button click.
	 */
	public void clickbtnCancel(){
		backButton.doClick();
	}
	
}
